import departmentAPI from "@/api/system/dept";

export default {
  state: {
    list: [],
    idMap: {},
  },
  getters: {
    oneById: (state) => (id) => {
      return state.idMap[id];
    },
  },
  mutations: {
    SET_ITEM: (state, item) => {
      state.list.push(item);
      const itemMap = {};
      itemMap[item.id] = item;
      state.idMap = Object.assign(state.idMap, itemMap);
    },
  },
  actions: {
    getDepartmentList({ commit }) {
      return new Promise((resolve) => {
        departmentAPI.list().then((res) => {
          for (var i = 0; i < res.data.length; i++) {
            const item = res.data[i];
            commit("SET_ITEM", item);
          }
          resolve(res.data);
        });
      });
    },
  },
};
